﻿using System.Collections.Generic;

namespace KuiHuaBaoDian.Services.Battle.Entity {

    /// <summary>
    /// 实体类型
    /// </summary>
    public readonly struct BattleEntityType {

        /// <summary>
        /// 未知
        /// </summary>
        public static readonly BattleEntityType Unknown = new() { Value = 0 };

        /// <summary>
        /// 单位
        /// </summary>
        public static readonly BattleEntityType Unit = new() { Value = 1 << 0 };

        /// <summary>
        /// 建筑
        /// </summary>
        public static readonly BattleEntityType Building = new() { Value = 1 << 1 };

        /// <summary>
        /// 子弹
        /// </summary>
        public static readonly BattleEntityType Bullet = new() { Value = 1 << 2 };

        /// <summary>
        /// 效果
        /// </summary>
        public static readonly BattleEntityType Effect = new() { Value = 1 << 3 };

        /// <summary>
        /// 技能
        /// </summary>
        public static readonly BattleEntityType Skill = new() { Value = 1 << 4 };

        /// <summary>
        /// 单元格
        /// </summary>
        public static readonly BattleEntityType Cell = new() { Value = 1 << 5 };

        /// <summary>
        /// 维持
        /// </summary>
        public static readonly BattleEntityType Holder = new() { Value = 1 << 6 };

        /// <summary>
        /// 玩家
        /// </summary>
        public static readonly BattleEntityType Player = new() { Value = 1 << 7 };

        /// <summary>
        /// 天赋
        /// </summary>
        public static readonly BattleEntityType Talent = new() { Value = 1 << 8 };

        /// <summary>
        /// 景观
        /// </summary>
        public static readonly BattleEntityType Landscape = new() { Value = 1 << 9 };

        private static readonly IReadOnlyDictionary<BattleEntityType, string> NAME_DICTIONARY = new Dictionary<BattleEntityType, string> {
            { Unknown, nameof(Unknown) },
            { Unit, nameof(Unit) },
            { Building, nameof(Building) },
            { Bullet, nameof(Bullet) },
            { Effect, nameof(Effect) },
            { Skill, nameof(Skill) },
            { Cell, nameof(Cell) },
            { Holder, nameof(Holder) },
            { Player, nameof(Player) },
            { Talent, nameof(Talent) },
            { Landscape, nameof(Landscape) },
        };

        /// <summary>
        /// 值
        /// </summary>
        public int Value { get; init; }

        public override bool Equals(object obj) {
            if (obj is BattleEntityType other) {
                return other.Value == Value;
            }
            return false;
        }

        public override int GetHashCode() => Value.GetHashCode();

        public static bool operator ==(BattleEntityType left, BattleEntityType right) => left.Equals(right);

        public static bool operator !=(BattleEntityType left, BattleEntityType right) => !left.Equals(right);

        public override string ToString() {
            if (NAME_DICTIONARY.TryGetValue(this, out var name)) {
                return name;
            } else {
                return $"{nameof(BattleEntityType)}({Value})";
            }
        }
    }
}
